# 博客详情页测试
import time

from selenium.webdriver.common.by import By
from common.Utils import BlogDriver


class BLogDetail:
    def __init__(self):
        url = ""
        driver = ""

        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    # 登录状态下的详情页测试
    def DetailTestByLogin(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        # 清除框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 查看全文
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > a").click()
        # 检查标题
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.title")
        # 检查内容
        self.driver.find_element(By.CSS_SELECTOR, "#detail > p")
        # 检查时间
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.date")
        # 检查详情页的按钮
        time.sleep(2)
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.operating > button:nth-child(1)").text
        assert text == "编辑"
        # BlogDriver.ScreenShot()
        BlogDriver.log("[debug] 登录状态下详情页访问成功")

    # 未登陆状态下博客详情页的测试
    def DetailTestByNoLogin(self):
        # 检查页面是否有注销按钮即可
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").text
        if text == "注销":
            BlogDriver.log("[error] 未登录情况下有‘注销’")
        # 屏幕截图
        BlogDriver.ScreenShot()
